package com.codingstyle.codingstylepoi.controller;

import com.codingstyle.codingstylepoi.Utils.PoiUtils;
import com.codingstyle.codingstylepoi.pojo.People;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2018/12/12 22:16
 */
@Controller
public class IndexController {
    @RequestMapping("/export")
    public void export(HttpServletResponse response) {

        //模拟从数据库获取需要导出的数据
        List<People> list = new ArrayList<>();
        for (int i = 1; i <= 200; i++) {
            People people = new People();
            people.setName("百度一下你就知道" + i);
            if (i % 3 == 2) {
                people.setSex("2");
            } else {
                people.setSex("1");
            }
            people.setBirthday(new Date());

            list.add(people);


        }
        //导出操作
        PoiUtils.exportExcel(list, "花名册", "草帽一伙", People.class, "海贼王.xls", response);
    }

    @RequestMapping("/importExcel")
    @ResponseBody
    public String importExcel() {
        String filePath = "F:\\海贼王.xls";
        //解析excel，
        List<People> personList = PoiUtils.importExcel(filePath, 1, 1, People.class);
        for (People people : personList) {
            System.out.println(people);
        }
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【" + personList.size() + "】行");

        //TODO 保存数据库
        return "success";
    }

}
